package com.mdx.tools.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author : jiagang
 * @date : Created in 2021/9/2 13:48
 */
public class GetKeyUtil {

    /**
     * 加签
     * @param map
     * @param privateKey
     * @return
     */
    public static String getSign(Map<String,Object> map,String privateKey){
        try {
            Set set = map.keySet();
            Iterator it = set.iterator();
            TreeMap tm = new TreeMap(new RSAUtil.MComparator());
            while (it.hasNext()) {
                String key = String.valueOf(it.next());
                tm.put(key, map.get(key));
            }

            String str = JSONObject.toJSONString(tm);
            byte[] signature = RSAUtil.sign(str.getBytes(), privateKey);
            return Base64.encodeBase64String(signature);

        }catch (Exception e){
//            throw new FlowException("加签失败");
            return null;
        }
    }
}
